﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelGenerator.BusinessObjects.SavingManagement;
using TravelGenerator.BusinessObjects.ModelObjects;
using TravelGenerator.BusinessObjects.ObjectValidator;
using TravelGenerator.IBusinessLayer.TravelManagement;

namespace TravelGenerator.BusinessLayer.TravelManagement
{
    public class AutomaticSavingElement : IAutomaticSavingElement
    {
        #region Properties

        /// <summary>
        /// Gets or sets the BL travel management.
        /// </summary>
        /// <value>The BL travel management.</value>
        private IBLTravelManagement BLTravelManagement
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the save parameter.
        /// </summary>
        /// <value>The save parameter.</value>
        private SaveParameter SaveParameter
        {
            get;
            set;
        }

        private bool reloadSaveResult = false;
        /// <summary>
        /// Gets or sets a value indicating whether [reload save result].
        /// </summary>
        /// <value><c>true</c> if [reload save result]; otherwise, <c>false</c>.</value>
        private bool ReloadSaveResult
        {
            get
            {
                return this.reloadSaveResult;
            }
            set
            {
                this.reloadSaveResult = value;
            }
        }
        private SaveResult saveResult;
        /// <summary>
        /// Gets the save result.
        /// </summary>
        /// <value>The save result.</value>
        private SaveResult SaveResult
        {
            get
            {
                if ((saveResult == null) || (this.ReloadSaveResult))
                {
                    this.saveResult = this.BLTravelManagement.GetSaveResult(this.SaveParameter);
                    this.ReloadSaveResult = false;
                }

                return this.saveResult;
            }            
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="AutomaticSavingElement"/> class.
        /// </summary>
        internal AutomaticSavingElement()
        {

        }

        #endregion

        #region IAutomaticSavingElement Members

        public void SetEntityToSave(IBLTravelManagement bl, IValidateEntity entity, Contact contact)
        {
            this.BLTravelManagement = bl;            

            this.SaveParameter = this.BLTravelManagement.GetSaveParameter(entity, contact);
        }

        /// <summary>
        /// Retourne une valeur permettant de savoir si la sauvegarde est possible.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance can save; otherwise, <c>false</c>.
        /// </returns>
        public bool CanSave()
        {
            return this.SaveResult.SaveError == SaveError.CanSave;
        }

        /// <summary>
        /// Obtient la raison de l'impossibilité de sauvegarder
        /// </summary>
        /// <returns></returns>
        public SaveError GetSaveError()
        {
            return this.SaveResult.SaveError;
        }

        /// <summary>
        /// Indique que l'utilisateur s'est connecté.
        /// </summary>
        /// <param name="contact">The contact.</param>
        public void UserConnected(Contact contact)
        {
            this.SaveParameter.Contact = contact;
            this.ReloadSaveResult = true;
        }

        public SaveEntityResult SaveEntity()
        {
            return this.BLTravelManagement.SaveEntity(this.SaveResult);
        }

        #endregion
    }
}
